﻿using CRM.Api.Common;
using CRM.Api.Filters;
using CRM.Api.Models;
using CRM.Infrastructure.Entity;
using CRM.Infrastructure.Service;
using CRM.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CRM.Api.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UsersController : BaseApiController
    {

        UsersService uService = new UsersService();
        ActionLogService alService = new ActionLogService();

        /// <summary>
        /// 系统登录
        /// </summary>
        /// <param name="model">用户登陆信息</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]

        public ResultData<Users> Login(LoginModel model)
        {
            ResultData<Users> rData = new ResultData<Users>();

            long userId = 0;

            if (string.IsNullOrEmpty(model.username))
            {
                rData.status = -1;
                rData.message = "请输入用户名";
            }
            else if (string.IsNullOrEmpty(model.password))
            {
                rData.status = -1;
                rData.message = "请输入密码";
            }
            else
            {

                var usr = uService.GetByLoginNum(model.username);
                if (usr == null)
                {
                    rData.message = "用户名不存在.";
                    rData.status = -1;
                }
                else if (MD5Helper.MD5String(model.username + model.password + "!!!") == usr.PassWord)
                {
                    rData.data = usr;
                    usr.PassWord = "";
                    userId = usr.UserID;
                }
                else
                {
                    rData.message = "密码错误.";
                    rData.status = -1;
                }
            }
            AddLoginLog(model, userId);

            return rData;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="model">重置密码信息</param>
        /// <returns></returns>
        [ActionCORS]
        [HttpPost]
        public ResultData<Users> ResetPassword(ResetPasswordModel model)
        {
            ResultData<Users> rData = new ResultData<Users>();
            var usr = uService.SingleOrDefault(model.UserID);
            if (usr == null)
            {
                rData.message = "用户名不存在.";
                rData.status = -1;
            }
            else if (MD5Helper.MD5String(usr.UserName + model.oldpwd + "!!!") == usr.PassWord )
            {
                rData.data = usr;
                usr.PassWord = MD5Helper.MD5String(usr.UserName + model.newpwd + "!!!");
                usr.UpdatedDate = DateTime.Now;
                uService.Update(usr);
            }
            else
            {
                rData.message = "原始密码错误.";
                rData.status = -1;
            }


            usr.PassWord = "";
            return rData;
        }

        /// <summary>
        /// 重置电话
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> ResetMobile(ResetMobileModel model)
        {
            var usr = uService.SingleOrDefault(model.UserID);           
            usr.Mobile = model.Mobile;
            usr.UpdatedDate = DateTime.Now;
            uService.Update(usr);
            return ResultData<string>.Success("更新成功");
        }

        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userId"></param>
        private void AddLoginLog(LoginModel model, long userId)
        {
            //登陆日志
            ActionLog login = new ActionLog
            {
                APPVersion = model.version,
                ClientId = model.clientid,
                Imei = model.imei,
                Moble = model.username,
                Model = model.model,
                UserID = userId,
                Platform = 1,
                PlatformVersion = model.osversion,
                Token = model.token,
                Uuid = model.uuid,
                Vendor = model.vendor,
                PostData = "Login",
                RequestMethod = "POST",
                ClientIP = IPHelper.IPAddress,
                RequestUrl = ActionContext.Request.RequestUri.AbsoluteUri,
                CreatedDate = DateTime.Now
            };
            alService.Insert(login);
        }

      
    }
}
